package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.constants.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.web.action.common.BaseAction;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class PromotionAction extends BaseAction<Promotion>{
	
	@Action(value="promotion_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		PageBean<Promotion> pageBean = WebClient
				.create("http://localhost:9001/bos_management/services/promotionService/pageQuery?page="+page+"&rows="+rows)
				.accept(MediaType.APPLICATION_JSON)
				.get(PageBean.class);
		
		ActionContext.getContext().getValueStack().push(pageBean);
		return SUCCESS;
	}
	
	@Action(value="promotion_showDetail")
	public String showDetail() throws IOException, TemplateException{
		//判断文件是否已经存在
		System.out.println("有没有执行。。。。。。。。。。。。。。。。。"+model.getId());
		String htmlRealPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
		File htmlFile = new File(htmlRealPath+"/"+model.getId()+".html");
		//文件不存在，查询数据库，结合freemarker 模板生成界面
		if(!htmlFile.exists()){
			//创建定时器对象,并加载
			Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
			configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath("WEB-INF/freemarker_templates")));
			//创建工作对象
			Template template = configuration.getTemplate("promotion_detail.ftl");
			/*//获得动态数据
			Promotion promotion = WebClient
					.create(Constants./bos_management_URL+"/bos_management/services/promotionService/promotion/"+model.getId())
					.accept(MediaType.APPLICATION_JSON).get(Promotion.class);*/
			// 动态数据
			Promotion promotion = WebClient
					.create(Constants.BOS_MANAGEMENT_HOST
							+ "/bos_management/services/promotionService/promotion/"
							+ model.getId()).accept(MediaType.APPLICATION_JSON)
					.get(Promotion.class);
			System.out.println("promotion---------"+promotion);
			Map<String, Object> paramMap = new HashMap<String,Object>();
			paramMap.put("promotion", promotion);
			//合并并输出
			template.process(paramMap, new OutputStreamWriter(new FileOutputStream(htmlFile),"utf-8"));
		}
		//如果存在，则直接返回页面
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		FileUtils.copyFile(htmlFile, ServletActionContext.getResponse().getOutputStream());
		return NONE;
	}
}
